package me.yan.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import me.yan.constant.Types;
import me.yan.constant.WebConst;
import me.yan.dto.cond.ContentCond;
import me.yan.pojo.ContentDomain;
import me.yan.service.content.ContentService;
import me.yan.utils.Commons;
import me.yan.utils.IPKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

@Tag(name="网页首页和关于页面")
@Controller
public class HomeController extends BaseController {

    @Autowired
    private ContentService contentService;



    @Hidden
    @GetMapping(value = {"/about", "/about/index"})
    public String getAbout(HttpServletRequest request,Model model) {
        this.blogBaseData(request, null);//获取友链
        request.setAttribute("active","about");
        model.addAttribute("commons",commons);
        return "site/about";
    }

    @Operation(summary = "作品主页")
    @GetMapping(value = {"/","/index"})
    public String index(HttpServletRequest request, @RequestParam(value = "limit", defaultValue = "12") int limit, Model model) {
        model.addAttribute("commons", commons);
        return this.index(1, limit, request,model);
    }

    @Operation(summary = "作品主页-分页")
    @GetMapping(value = "/photo/page/{p}")
    public String index(
            @PathVariable(name = "p")
            int page,
            @RequestParam(name = "limit", required = false, defaultValue = "9999")
            int limit,
            HttpServletRequest request,
            Model model
    ){
        page = page < 0 || page > 100 ? 1 : page;
        ContentCond contentCond = new ContentCond();
        contentCond.setType(Types.PHOTO.getType());
        PageInfo<ContentDomain> articles = null;
        try {
            articles = contentService.getArticlesByCond(contentCond, page, limit);
        } catch (Exception e) {
            e.printStackTrace();
        }

        request.setAttribute("archives", articles);
        System.out.println("articles:////" + articles);
        request.setAttribute("active", "work");
        model.addAttribute("commons", commons);
        return "site/index";
    }

    @Operation(summary = "作品内容")
    @GetMapping(value = "/photo/article/{cid}")
    public String article(
            @PathVariable("cid")
            Integer cid,
            HttpServletRequest request,
            Model model
    ){
        ContentDomain article = contentService.getArticleById(cid);
        //更新文章的点击量
        this.updateArticleHit(article.getCid(),article.getHits());
        request.setAttribute("archive", article);
        request.setAttribute("active","work");
        model.addAttribute("commons", commons);
        return "site/works-details";
    }
    /**
     * 更新文章的点击率
     *
     * @param cid
     * @param chits
     */
    private void updateArticleHit(Integer cid, Integer chits) {
        Integer hits = cache.hget("article", "hits");
        if (chits == null) {
            chits = 0;
        }
        hits = null == hits ? 1 : hits + 1;
        if (hits >= WebConst.HIT_EXCEED) {
            ContentDomain temp = new ContentDomain();
            temp.setCid(cid);
            temp.setHits(chits + hits);
            contentService.updateContentByCid(temp);
            cache.hset("article", "hits", 0);
        } else {
            cache.hset("article", "hits", hits);
        }
    }
}
